package com.wsq.calculate;

import com.wsq.function.DisplayLogic;
import com.wsq.listener.EventListener;
import com.wsq.ui.CalculateDisplay;
import com.wsq.ui.SidebarView;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Calculate extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	private CalculateDisplay mInput;
	private TextView mOperator;
	private TextView mDegree;

	private RadioGroup mRadioGroup;

	private static final int HVGA_WIDTH_PIXELS = 480;

	public EventListener eventListener = new EventListener();

	private DisplayLogic mHandler;

	private SidebarView mMainView;

	private ImageView mToggleSidebar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculate);
		findViews();
		eventListener
				.setOrientation(getResources().getConfiguration().orientation);
		if (savedInstanceState == null) {
			mHandler = new DisplayLogic(this, mInput, mOperator,mDegree);
			eventListener.setLogic(mHandler);
			eventListener.setWidgets(this);
			mInput.setOnKeyListener(eventListener);
			if (mMainView != null)
				mMainView.change();
		} else {
			mHandler = savedInstanceState.getParcelable("logic");
			String inputString = savedInstanceState.getString("input");
			String operatorString = savedInstanceState.getString("operator");
			eventListener.setLogic(mHandler);
			eventListener.setWidgets(this);
			mHandler.setDisplay4ConfigurationChange(mInput, mOperator,mDegree);
			mInput.setText(inputString);
			mOperator.setText(operatorString);
			mInput.setOnKeyListener(eventListener);
			if (mMainView != null)
				mMainView.change();
		}
	}

	private void findViews() {
		mInput = (CalculateDisplay) findViewById(R.id.idCalculateDisplay);
		mOperator = (TextView) findViewById(R.id.idCalOperator);
		mDegree = (TextView) findViewById(R.id.idCalDegree);
		mRadioGroup = (RadioGroup) findViewById(R.id.idCalRadGroup);
		mRadioGroup.setOnCheckedChangeListener(this);
		mMainView = (SidebarView) findViewById(R.id.idCalculateMain);
		mToggleSidebar = (ImageView) findViewById(R.id.idKeyboardToggle);
		if (mToggleSidebar != null)
			mToggleSidebar.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.idKeyboardToggle:
			if (mMainView != null)
				mMainView.onToggle((ImageView) v);
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.idMenuSetting:
			Intent intent = new Intent(this, Setting.class);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * The font sizes in the layout files are specified for a HVGA display.
	 * Adjust the font sizes accordingly if we are running on a different
	 * display.
	 */
	public void adjustFontSize(TextView view) {
		float fontPixelSize = view.getTextSize();
		Display display = getWindowManager().getDefaultDisplay();
		int h = Math.min(display.getWidth(), display.getHeight());
		float ratio = (float) h / HVGA_WIDTH_PIXELS;
		view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		outState.putParcelable("logic", mHandler);
		outState.putString("input", mInput.getText().toString());
		outState.putString("operator", mOperator.getText().toString());
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch (checkedId) {
		case R.id.radioButton1:
			mDegree.setText("DEG");
			break;
		case R.id.radioButton2:
			mDegree.setText("RAD");
			break;
		case R.id.radioButton3:
			mDegree.setText("GRAD");
			break;
		}
	}

}
